package com.alibaba.algo.apiimpl.feign;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.common.SysConstant;
import com.alibaba.algo.cooperation.feign.service.DictFeignService;
import com.alibaba.algo.cooperation.feign.vo.enums.SysDictCodeEnum;
import com.alibaba.algo.cooperation.feign.vo.req.SelectDictListRequestVO;
import com.alibaba.algo.cooperation.feign.vo.res.SysDictResponseVO;
import com.alibaba.algo.domain.dict.SysDictDomain;
import com.alibaba.algo.entity.dict.SysDict;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author Fan.tianpeng
 * @date 2021/5/8 10:42
 */
@RestController
public class DictFeignServiceImpl implements DictFeignService {

    @Resource
    private SysDictDomain sysDictDomain;


    @Override
    public BaseResult selectPage(SelectDictListRequestVO param) {
        LinkedHashMap<String, Object> sortMap = new LinkedHashMap<>();
        sortMap.put("sort", "ASC");

        SysDict sysDict = new SysDict();
        sysDict.setNameKey(param.getNameKey());
        sysDict.setRootDict(param.isRootDict());
        sysDict.setCodeKey(param.getCodeKey());
        sysDict.setParentCode(param.getParentCode());

        return BaseResult.buildSuccess(sysDictDomain.selectPage(sysDict, param.getPageIndex(), param.getPageSize(), sortMap));

    }

    @Override
    public BaseResult<List<SysDictResponseVO>> selectList(SelectDictListRequestVO param) {
        LinkedHashMap<String, Object> sortMap = new LinkedHashMap<>();
        sortMap.put("sort", "ASC");
        SysDict sysDict = new SysDict();
        sysDict.setParentCode(param.getParentCode());
        String codeKey = param.getCodeKey();
        if (codeKey != null && !codeKey.isEmpty()) {
            sysDict.setCodeKey("%" + codeKey + "%");
        }
        String nameKey = param.getNameKey();
        if (nameKey != null && !nameKey.isEmpty()) {
            sysDict.setNameKey("%" + nameKey + "%");
        }
        sysDict.setRootDict(param.isRootDict());
        List<SysDict> sysDictList = sysDictDomain.listByPara(sysDict, sortMap);

        List<SysDictResponseVO> resourceResponseVOList = new ArrayList<>();
        sysDictList.forEach(sysRes -> {
            SysDictResponseVO sysDictResponseVO = new SysDictResponseVO();
            BeanUtils.copyProperties(sysRes, sysDictResponseVO);
            resourceResponseVOList.add(sysDictResponseVO);
        });
        return BaseResult.buildSuccess(resourceResponseVOList);
    }

    /**
     * 查询页面上适用的一些数据字典，比如浏览器标题，系统名称等
     */
    @Override
    public BaseResult<List<SysDictResponseVO>> selectPageDict() {
        SelectDictListRequestVO param = new SelectDictListRequestVO();
        param.setParentCode(SysDictCodeEnum.PAGE_DICT.getCode());
        BaseResult<List<SysDictResponseVO>> result = selectList(param);
        if (result.getCode().equals(SysConstant.OK)) {
            List<SysDictResponseVO> data = result.getData();
            if (data == null) {
                data = new ArrayList<>();
            }
            out:
            for (SysDictCodeEnum dict : SysDictCodeEnum.values()) {
                for (SysDictResponseVO temp : data) {
                    if (temp.getCode().equals(dict.getCode())) {
                        continue out;
                    }
                }
                SysDictResponseVO temp = new SysDictResponseVO();
                temp.setCode(dict.getCode());
                temp.setParentCode(dict.getParentCode());
                temp.setDesc(dict.getDesc());
                temp.setName(dict.getName());
                temp.setSort(dict.getSort());
                data.add(temp);
            }
        }
        return result;
    }
}
